package controller

import (
	"strconv"

	"github.com/gogf/gf/v2/net/ghttp"

	"goldtree/internal/service"
	"goldtree/utility/response"
)

// StockPoolController 股票池控制器
type StockPoolController struct{}

var StockPoolCtrl = StockPoolController{}

// Create 创建股票池
func (c *StockPoolController) Create(r *ghttp.Request) {
	var req *service.CreatePoolReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	result, err := service.StockPool.Create(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// Update 更新股票池
func (c *StockPoolController) Update(r *ghttp.Request) {
	var req *service.UpdatePoolReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	err := service.StockPool.Update(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r)
}

// Delete 删除股票池
func (c *StockPoolController) Delete(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "股票池ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "股票池ID格式错误")
		return
	}

	err = service.StockPool.Delete(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r)
}

// GetById 根据ID获取股票池
func (c *StockPoolController) GetById(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "股票池ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "股票池ID格式错误")
		return
	}

	result, err := service.StockPool.GetById(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	if result == nil {
		response.ErrorExit(r, "股票池不存在")
		return
	}

	response.SuccessExit(r, result)
}

// GetDetail 获取股票池详情
func (c *StockPoolController) GetDetail(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "股票池ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "股票池ID格式错误")
		return
	}

	result, err := service.StockPool.GetDetail(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetList 获取股票池列表
func (c *StockPoolController) GetList(r *ghttp.Request) {
	var req *service.GetPoolListReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	result, err := service.StockPool.GetList(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.PageExit(r, result.List, result.Total, result.Page, result.Limit)
}

// GetActiveList 获取激活的股票池列表
func (c *StockPoolController) GetActiveList(r *ghttp.Request) {
	result, err := service.StockPool.GetActivePools(r.Context())
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// AddMember 添加股票池成员
func (c *StockPoolController) AddMember(r *ghttp.Request) {
	var req *service.AddMemberReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	err := service.StockPool.AddMember(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, "添加股票池成员成功")
}

// BatchAddMembers 批量添加股票池成员
func (c *StockPoolController) BatchAddMembers(r *ghttp.Request) {
	var req *service.BatchAddMemberReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	err := service.StockPool.BatchAddMembers(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, "批量添加股票池成员成功")
}

// RemoveMember 移除股票池成员
func (c *StockPoolController) RemoveMember(r *ghttp.Request) {
	poolIdStr := r.Get("pool_id").String()
	stockCode := r.Get("stock_code").String()

	if poolIdStr == "" {
		response.ErrorExit(r, "股票池ID不能为空")
		return
	}

	if stockCode == "" {
		response.ErrorExit(r, "股票代码不能为空")
		return
	}

	poolId, err := strconv.Atoi(poolIdStr)
	if err != nil {
		response.ErrorExit(r, "股票池ID格式错误")
		return
	}

	err = service.StockPool.RemoveMember(r.Context(), poolId, stockCode)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, "移除股票池成员成功")
}

// GetMembers 获取股票池成员列表
func (c *StockPoolController) GetMembers(r *ghttp.Request) {
	poolIdStr := r.Get("pool_id").String()
	if poolIdStr == "" {
		response.ErrorExit(r, "股票池ID不能为空")
		return
	}

	poolId, err := strconv.Atoi(poolIdStr)
	if err != nil {
		response.ErrorExit(r, "股票池ID格式错误")
		return
	}

	pageStr := r.Get("page").String()
	page := 1
	if pageStr != "" {
		page, err = strconv.Atoi(pageStr)
		if err != nil || page < 1 {
			page = 1
		}
	}

	limitStr := r.Get("limit").String()
	limit := 20
	if limitStr != "" {
		limit, err = strconv.Atoi(limitStr)
		if err != nil || limit < 1 || limit > 100 {
			limit = 20
		}
	}

	members, total, err := service.StockPool.GetMembers(r.Context(), poolId, page, limit)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.PageExit(r, members, total, page, limit)
}